<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ShopBanner extends Model
{
    use HasFactory;

    protected $table = 'sp_shop_banner';
    protected $primaryKey = 'id';
    public $timestamps = false;

    //商户轮播状态
    const SHOP_BANNER_STATUS_ENABLE = 1;//启用
    const SHOP_BANNER_STATUS_DISABLE = 2;//禁用

    //商户轮播是否删除 0未删除  1已删除
    const SHOP_BANNER_IS_DELETE = 1;//已删除
    const SHOP_BANNER_NOT_DELETE = 0;//未删除

    //查询店铺页面轮播图
    protected function GetShopBannerByMemberid($member_id)
    {
        $where = array(
            'b.status' => self::SHOP_BANNER_STATUS_ENABLE, //1启用  2 禁用
            'b.is_delete' => self::SHOP_BANNER_NOT_DELETE, //0未删除  1已删除
            'b.member_id' => $member_id,
        );
        $data = $this->from('sp_shop_banner as b')
            ->leftjoin('sp_goods_2 as g','g.id','b.goods_id')
            ->where($where)
            ->select('b.banner_img','b.goods_id','g.status')
            ->orderBy('b.sort', 'asc')
            ->limit(5)
            ->get();
        $data = $data ? $data->toArray() : array();
        return $data;
    }

    //店铺轮播图设置列表
    protected function GetShopBannerList($pageSize, $where)
    {
        $select = array('sb.id', 'sb.sort', 'sb.banner_img', 'sb.create_time', 'sb.status', 'sb.goods_id', 'g.title');
        $data = $this
            ->from('sp_shop_banner as sb')
            ->leftjoin('sp_goods_2 as g', 'g.id', 'sb.goods_id')
            ->select($select)
            ->where($where)
            ->orderBy('sb.sort', 'asc')
            ->paginate($pageSize);
        $data = $data ? $data->toArray() : array();
        return $data;
    }

    //根据id查询
    protected function getShopBannerById($id)
    {
        $where = array(
            'sb.id' => $id,
        );
        $select = array('sb.id', 'sb.sort', 'sb.banner_img', 'sb.create_time', 'sb.status', 'sb.goods_id', 'g.title', 'g.imgurl');
        $data = $this
            ->from('sp_shop_banner as sb')
            ->leftjoin('sp_goods_2 as g', 'g.id', 'sb.goods_id')
            ->select($select)
            ->where($where)
            ->first();
        $data = $data ? $data->toArray() : array();
        return $data;
    }

    //根据id查询
    protected function GetShopCategoryById($id)
    {
        $where = array(
            'id' => $id,
        );
        $data = $this
            ->where($where)
            ->first();
        $data = $data ? $data->toArray() : [];
        return $data;
    }

    //添加
    protected function addData($data)
    {
        return $data = $this->insert($data);
    }

    //根据id修改
    protected function editDataById($id, $data)
    {
        $where = array(
            'id' => $id,
        );
        return $data = $this->where($where)->update($data);
    }
}
